﻿namespace Dissemination.MVC.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Web.Mvc;
    using System.Web.UI.WebControls;

    using Dissemination.MVC.Repositories;
    using Dissemination.MVC.ViewModels;
    using Dissemination.MVC.Models;
    using System.Linq;
    using Dissemination.MVC.Controllers.Extensions;
    using System.Globalization;

    public class SeminarController : ControllerBase
    {
        protected SeminarRepository repository = new SeminarRepository();

        #region Details

        //
        // GET: /Seminar/Details/5

        /// <summary>
        /// Returns the view associated to the details of an entity in the data source.
        /// </summary>
        /// <param name="id">The entity id.</param>
        /// <returns>Details view</returns>
        [Authorize(Roles = "Demostrator,Reporter,Administrator")]
        public ActionResult Details(int id)
        {
            if (log.IsDebugEnabled) log.DebugFormat("Entering Seminar details with id={0}...", id);
            Seminar seminar = repository.Get(id);
            
            if (seminar == null)
            {
                if (log.IsErrorEnabled) log.ErrorFormat("Seminar with id={0} not found", id);
                return View("NotFound");
            }

            if (log.IsDebugEnabled) log.DebugFormat("Leaving Seminar details with id={0}...", id);
            return View(seminar);
        }

        //TODO: FollowUp(int id) ?????
        [Authorize(Roles = "Demostrator,Reporter,Administrator")]
        public ActionResult FollowUp(int id)
        {
            Seminar seminar = repository.GetFollowUpReport(id);

            var viewModel = new SeminarFollowUpReportViewModel();

            viewModel.Seminar = new Seminar
            {
                SeminarID = id
            };

            viewModel.NumberOfAssessments = seminar.FollowUps.Count;
            viewModel.NumberOfFemales = seminar.FollowUps.Count(p => p.Gender.GenderName == "Female");
            viewModel.NumberOfMales = seminar.FollowUps.Count(p => p.Gender.GenderName == "Male");
            viewModel.AverageAge = Enumerable.Average(seminar.FollowUps, p => p.FollowUpAge) ==null? 0 : Enumerable.Average(seminar.FollowUps, p => p.FollowUpAge).Value;

            viewModel.SeminarFollowUpReportBackgrounds = new List<ReportPercentageRowViewModel>();

            List<ProfessionalBackground> professionalBackgrounds = repository.GetProfessionalBackgroundsList();
            int total = viewModel.NumberOfFemales + viewModel.NumberOfMales;
            foreach (var professionalBackground in professionalBackgrounds)
            {
                ProfessionalBackground backgroundTemp = professionalBackground;
                int count =
                        seminar.FollowUps.Count(
                                p =>
                                p.ProfessionalBackground.ProfessionalBackgroundID
                                == backgroundTemp.ProfessionalBackgroundID);
                double percentage = (double)count / total;

                var background = new ReportPercentageRowViewModel { Number = count, Percentage = percentage, Text = backgroundTemp.ProfessionalBackgroundName };
                viewModel.SeminarFollowUpReportBackgrounds.Add(background);
            }

            viewModel.SeminarFollowUpReportSpecialities = new List<ReportPercentageRowViewModel>();
            List<Speciality> specialities = repository.GetSpecialitiesList();
            foreach (var speciality in specialities)
            {
                Speciality specialityTemp = speciality;
                int count = seminar.FollowUps.Count(
                        p=>
                        p.Speciality.SpecialityID == specialityTemp.SpecialityID
                    );
                double percentage = (double)count / total;
                var spec = new ReportPercentageRowViewModel
                               {Text = specialityTemp.SpecialityName, Number = count, Percentage = percentage};
                viewModel.SeminarFollowUpReportSpecialities.Add(spec);
            }

            viewModel.SeminarFollowUpReportRoles = new List<ReportPercentageRowViewModel>();
            ListItemCollection roles = repository.GetRoles();
            foreach (ListItem role in roles)
            {
                ListItem roleTemp = role;
                int count = seminar.FollowUps.Count(
                    fu =>
                        fu.FollowUpTakePart == int.Parse(roleTemp.Value)
                    );
                double percentage = (double)count / total;
                var row = new ReportPercentageRowViewModel
                              {Text = roleTemp.Text, Number = count, Percentage = percentage};

                viewModel.SeminarFollowUpReportRoles.Add(row);
            }

            viewModel.SeminarFollowUpReportAttitude = new List<ReportPercentageRowViewModel>();
            ListItemCollection attitudes = repository.GetAttitudes();
            foreach (ListItem attitude in attitudes)
            {
                ListItem attitudeTemp = attitude;
                int count = seminar.FollowUps.Count(
                    fu =>
                        fu.FollowUpAttitude == int.Parse(attitudeTemp.Value)
                    );
                double percentage = (double)count / total;
                var row = new ReportPercentageRowViewModel { Text = attitudeTemp.Text, Number = count, Percentage = percentage };

                viewModel.SeminarFollowUpReportAttitude.Add(row);
            }

            viewModel.SeminarFollowUpReportConfident = new List<ReportListRowViewModel>();
            ListItemCollection confidents = repository.GetConfidents();
            foreach (ListItem confident in confidents)
            {
                var row = new ReportListRowViewModel { Text = confident.Text, Numbers = new List<int>() };
                for (int i = 1; i <= 5; i++)
                {
                    int? index = i;
                    int count = 0;
                    //TODO: Do it using reflection
                    switch (confident.Value)
                    {
                        case "1":
                            count = seminar.FollowUps.Count(
                                fu =>
                                    fu.FollowUpConfident1 == index
                                );
                            break;
                        case "2":
                            count = seminar.FollowUps.Count(
                                fu =>
                                    fu.FollowUpConfident2 == index
                                );
                            break;
                        case "3":
                            count = seminar.FollowUps.Count(
                                fu =>
                                    fu.FollowUpConfident3 == index
                                );
                            break;
                        case "4":
                            count = seminar.FollowUps.Count(
                                fu =>
                                    fu.FollowUpConfident4 == index
                                );
                            break;
                        case "5":
                            count = seminar.FollowUps.Count(
                                fu =>
                                    fu.FollowUpConfident5 == index
                                );
                            break;
                        case "6":
                            count = seminar.FollowUps.Count(
                                fu =>
                                    fu.FollowUpConfident6 == index
                                );
                            break;
                        case "7":
                            count = seminar.FollowUps.Count(
                                fu =>
                                    fu.FollowUpConfident7 == index
                                );
                            break;
                        case "8":
                            count = seminar.FollowUps.Count(
                                fu =>
                                    fu.FollowUpConfident8 == index
                                );
                            break;
                        case "9":
                            count = seminar.FollowUps.Count(
                                fu =>
                                    fu.FollowUpConfident9 == index
                                );
                            break;
                        case "10":
                            count = seminar.FollowUps.Count(
                                fu =>
                                    fu.FollowUpConfident10 == index
                                );
                            break;
                    }

                    row.Numbers.Add(count);
                }

                viewModel.SeminarFollowUpReportConfident.Add(row);
            }

            return View(viewModel);
        }

        #endregion

        #region New

        //
        // GET: /Seminar/Create

        /// <summary>
        /// Returns the view associated with an empty entity
        /// </summary>
        /// <param name="targetAreaID"></param>
        /// <returns>Create view</returns>
        [Authorize(Roles = "Reporter,Administrator")]
        public ActionResult Create(int targetAreaID)
        {
            if (log.IsDebugEnabled) log.Debug("Entering Seminar creation (GET)...");
            Seminar seminar = new Seminar();
            seminar.TargetArea = new TargetArea
            {
                TargetAreaID = targetAreaID,
                Program = repository.GetProgramByTargetAreaID(targetAreaID)
            };

            if (log.IsDebugEnabled) log.Debug("Leaving Seminar creation (GET)..."); 
            return View(seminar);
        } 

        //
        // POST: /Seminar/Create

        /// <summary>
        /// Creates the specified entity.
        /// </summary>
        /// <param name="seminar">The entity</param>
        /// <returns>Details view</returns>
        [HttpPost]
        [Authorize(Roles = "Reporter,Administrator")]
        public ActionResult Create(FormCollection collection)
        {
            if (log.IsDebugEnabled) log.Debug("Entering Seminar creation (POST)...");

            Seminar seminar = new Seminar();

            try
            {
                IFormatProvider culture = new CultureInfo("es-ES", true);
                seminar.SeminarDate = DateTime.ParseExact(collection["SeminarDate"], "dd-MM-yyyy", culture);
            }
            catch
            {
                seminar.SeminarDate = null;
                ModelState.AddModelError("SeminarDate", "The entered date was incorrect");
            }

            seminar.SeminarCenter = collection["SeminarCenter"];
            seminar.SeminarCity = collection["SeminarCity"];

            int seminarTargetAreaID = int.Parse(Request["TargetAreaID"]);
            seminar.TargetArea = repository.GetTargetAreaByID(seminarTargetAreaID);

            if (ModelState.IsValid)
            {

                repository.Add(seminar);
                repository.Save();

                if (log.IsDebugEnabled) log.Debug("Leaving Seminar creation (POST)...");
                return RedirectToAction("IndexOrDetails", "ReporterManager", new { targetAreaID = seminarTargetAreaID });
            }

            if (log.IsDebugEnabled)
            {
                //TODO: Completar Create_Seminar
            }


            return View(seminar);
        }

        #endregion

        #region Edit

        //
        // GET: /Seminar/Edit/5

        /// <summary>
        /// Returns the view associated with the edit of an existing entity.
        /// </summary>
        /// <param name="id">The id of the entity to edit</param>
        /// <returns>Edit view</returns>
        [Authorize(Roles = "Reporter,Administrator")]
        public ActionResult Edit(int id)
        {
            if (log.IsDebugEnabled) log.DebugFormat("Entering Seminar with id {0} edit (GET)...", id);
            Seminar seminar = repository.Get(id);
            
            if (seminar == null)
            {
                if (log.IsErrorEnabled) log.ErrorFormat("Seminar with id={0} not found", id);
                return View("NotFound");
            }

            if (log.IsDebugEnabled) log.DebugFormat("Leaving Seminar with id {0} edit (GET)...", id);
            return View(seminar);
        }

        //
        // POST: /Seminar/Edit/5

        /// <summary>
        /// Edits the specified entity.
        /// </summary>
        /// <param name="id">The id of the existing entity.</param>
        /// <param name="collection">The collection of values entered.</param>
        /// <returns>Details view</returns>
        [HttpPost]
        [Authorize(Roles = "Reporter,Administrator")]
        public ActionResult Edit(int id, FormCollection collection)
        {
            if (log.IsDebugEnabled) log.DebugFormat("Entering Seminar with id {0} edit (POST)...", id);
            Seminar seminar = repository.Get(id);
            
            try
            {

                IFormatProvider culture = new CultureInfo("es-ES", true);
                seminar.SeminarDate = DateTime.ParseExact(collection["SeminarDate"], "dd-MM-yyyy", culture);
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("SeminarDate", ex);
                if (log.IsErrorEnabled) log.Error("Error editing Seminar", ex);
            }

            seminar.SeminarCenter = collection["SeminarCenter"];
            seminar.SeminarCity = collection["SeminarCity"];
            if (ModelState.IsValid)
            {
                repository.Save();

                if (log.IsDebugEnabled) log.DebugFormat("Leaving Seminar with id {0} edit (POST)...", id);
                return RedirectToAction("IndexOrDetails", "ReporterManager", new { targetAreaID = seminar.TargetArea.TargetAreaID });
            }
            else
            {
                return View(seminar);
            }

        }

        #endregion

        #region Delete

        //
        // GET: /Seminar/Delete/5

        /// <summary>
        /// Returns the view associated with a given entity to be deleted
        /// </summary>
        /// <param name="id">The id of the existing entity</param>
        /// <returns>Delete view</returns>
        [Authorize(Roles = "Reporter,Administrator")]
        public ActionResult Delete(int id)
        {
            throw new NotImplementedException();

            if (log.IsDebugEnabled) log.DebugFormat("Entering Seminar with id {0} delete (GET)...", id);
            Seminar seminar = repository.Get(id);

            if (seminar == null)
            {
                if (log.IsErrorEnabled) log.ErrorFormat("Seminar with id={0} not found", id);
                return View("NotFound");
            }

            if (log.IsDebugEnabled) log.DebugFormat("Leaving Seminar with id {0} delete (GET)...", id);
            return View(seminar);
        }

        //
        // POST: /Seminar/Delete/5

        /// <summary>
        /// Deletes the specified entity.
        /// </summary>
        /// <param name="id">The id of the existing entity</param>
        /// <param name="confirmButton">The confirm button.</param>
        /// <returns>Deleted view</returns>
        [HttpPost]
        [Authorize(Roles = "Reporter,Administrator")]
        public ActionResult Delete(int id, string confirmButton)
        {
            throw new NotImplementedException();

            if (log.IsDebugEnabled) log.DebugFormat("Entering Seminar with id {0} delete (POST)...", id);
            Seminar seminar = repository.Get(id);

            if (seminar == null)
            {
                if (log.IsErrorEnabled) log.ErrorFormat("Seminar with id={0} not found", id);
                return View("NotFound");
            }

            repository.Delete(seminar);
            repository.Save();

            if (log.IsDebugEnabled) log.DebugFormat("Leaving Seminar with id {0} delete (POST)...", id);
            return RedirectToAction("IndexOrDetails", "ReporterManager", new { targetAreaID = seminar.TargetArea.TargetAreaID });

        }

        #endregion

        #region Reports

        //
        // GET: /Seminar/Participants/5

        [Authorize(Roles = "Demostrator,Reporter,Administrator")]
        public ActionResult Participants(int seminarID)
        {
            if (log.IsDebugEnabled) log.DebugFormat("Leaving Program with id {0} ta...", seminarID);

            var seminar = repository.Get(seminarID);
            var program = seminar.TargetArea.Program;
            var ta = seminar.TargetArea;

            List<ExcelCellChange> excelChanges = new List<ExcelCellChange>();
            List<ExcelSheetInformation> sheets = new List<ExcelSheetInformation>();

            excelChanges.Add(new ExcelCellChange { Row = 2, Column = 2, Value = program.ProgramName });
            excelChanges.Add(new ExcelCellChange { Row = 3, Column = 2, Value = "Participants - " + seminar.FriendlyTitle });

            int row = 6;
            foreach (Participant p in seminar.Participants)
            {
                excelChanges.AddRange(new List<ExcelCellChange>
                            {
                                    new ExcelCellChange { Row = row, Column = 2, Value = ta.Country.CountryName },
                                    new ExcelCellChange { Row = row, Column = 3, Value = ta.TargetAreaCode },
                                    new ExcelCellChange { Row = row, Column = 5, Value = p.ParticipantName },
                                    new ExcelCellChange { Row = row, Column = 6, Value = p.ParticipantSurname },
                                    new ExcelCellChange { Row = row, Column = 7, Value = p.ParticipantEmail },
                                    new ExcelCellChange { Row = row, Column = 8, Value = p.Gender.GenderName },
                                    new ExcelCellChange { Row = row, Column = 9, Value = p.ParticipantAge.ToString() },
                                    new ExcelCellChange { Row = row, Column = 11, Value = p.ParticipantDepartment },
                                    new ExcelCellChange { Row = row, Column = 12, Value = p.ParticipantPosition },
                            });
                row++;
            }


            ExcelSheetInformation sheet = new ExcelSheetInformation() { Name = "Participants", Changes = excelChanges };
            sheets.Add(sheet);
            var changes = sheets.AsQueryable();
            string temporaryFileName = Server.MapPath(@"~/temp/" + Guid.NewGuid() + ".xlsx");

            if (log.IsDebugEnabled) log.DebugFormat("Leaving Program with id {0} ta...", seminarID);

            return this.Excel("EOD_Participants.xlsx", temporaryFileName, Server.MapPath(@"~/templates/EOD_Participants.xlsx"), "Participants", changes);
        }

        //
        // GET: /Seminar/Participants/5

        [Authorize(Roles = "Demostrator,Reporter,Administrator")]
        public ActionResult TestResults(int seminarID)
        {
            var seminar = repository.Get(seminarID);

            List<ExcelSheetInformation> sheets = new List<ExcelSheetInformation>();
            List<ExcelCellChange> excelFistPageChanges = new List<ExcelCellChange>();


            int testAnsweredPerSeminar = seminar.TestResults.Count;
            int testCorrectPerSeminar = seminar.TestResults.Sum(ts => ts.TestResultCorrectAnswers);
            int testIncorrectPerSeminar = seminar.TestResults.Sum(ts => ts.TestResultIncorrectAnswers);
            int testNoAnsweredPerSeminar = seminar.TestResults.Sum(ts => ts.TestResultNotAnswered);

            excelFistPageChanges.AddRange(new List<ExcelCellChange>
                        {
                        new ExcelCellChange { Row = 2, Column = 2, Value = seminar.TargetArea.Program.ProgramName },
                        new ExcelCellChange { Row = 3, Column = 2, Value = "TEST RESULTS - " + seminar.FriendlyTitle },
                        new ExcelCellChange { Row = 5, Column = 3, Value = testAnsweredPerSeminar },
                        new ExcelCellChange { Row = 7, Column = 3, Value = testCorrectPerSeminar },
                        new ExcelCellChange { Row = 8, Column = 3, Value = testIncorrectPerSeminar },
                        new ExcelCellChange { Row = 9, Column = 3, Value = testNoAnsweredPerSeminar },
                        });

            ExcelSheetInformation sheet = new ExcelSheetInformation() { Name = "Seminar Test Results", Changes = excelFistPageChanges };
            sheets.Add(sheet);

        
            string temporaryFileName = Server.MapPath(@"~/temp/" + Guid.NewGuid() + ".xlsx");

            if (log.IsDebugEnabled) log.DebugFormat("Leaving Program with id {0} ta...", seminarID);

            return this.Excel("seminar_testresults_report.xlsx", temporaryFileName, Server.MapPath(@"~/templates/EOD_Single_Seminar_Tests_Results.xlsx"), "EODs Test Results SUMMARY", sheets.AsQueryable());

                       
        }

        //
        // GET: /Seminar/Participants/5

        [Authorize(Roles = "Demostrator,Reporter,Administrator")]
        public ActionResult FollowUps(int seminarID)
        {
            if (log.IsDebugEnabled) log.DebugFormat("Leaving Program with id {0} ta...", seminarID);

            var s = repository.Get(seminarID);
            var program = s.TargetArea.Program;
            var ta = s.TargetArea;

            List<ExcelCellChange> excelChanges = new List<ExcelCellChange>();
            List<ExcelSheetInformation> sheets = new List<ExcelSheetInformation>();

            excelChanges.AddRange(new List<ExcelCellChange>
                        {
                            new ExcelCellChange { Row = 2, Column = 2, Value = ta.Program.ProgramName },
                            new ExcelCellChange { Row = 3, Column = 2, Value = "FOLLOW UP - " + s.FriendlyTitle },
                            new ExcelCellChange { Row = 5, Column = 5, Value = s.FollowUps.Count },
                            new ExcelCellChange { Row = 8, Column = 3, Value = s.FollowUps.Average(fu => fu.FollowUpAge)??0 },
                            new ExcelCellChange { Row = 9, Column = 3, Value = s.FollowUps.Count(fu => fu.Gender.GenderName == "Male") },
                            new ExcelCellChange { Row = 10, Column = 3, Value = s.FollowUps.Count(fu => fu.Gender.GenderName == "Female") },
                            new ExcelCellChange { Row = 13, Column = 2, Value = s.FollowUps.Count(fu => fu.ProfessionalBackground.ProfessionalBackgroundID == 4) },
                            new ExcelCellChange { Row = 14, Column = 2, Value = s.FollowUps.Count(fu => fu.ProfessionalBackground.ProfessionalBackgroundID == 5) },
                            new ExcelCellChange { Row = 15, Column = 2, Value = s.FollowUps.Count(fu => fu.ProfessionalBackground.ProfessionalBackgroundID == 3) },
                            new ExcelCellChange { Row = 16, Column = 2, Value = s.FollowUps.Count(fu => fu.ProfessionalBackground.ProfessionalBackgroundID == 6) },
                            new ExcelCellChange { Row = 17, Column = 2, Value = s.FollowUps.Count(fu => fu.ProfessionalBackground.ProfessionalBackgroundID == 2) },
                            new ExcelCellChange { Row = 18, Column = 2, Value = s.FollowUps.Count(fu => fu.ProfessionalBackground.ProfessionalBackgroundID == 7) },
                            new ExcelCellChange { Row = 21, Column = 2, Value = s.FollowUps.Count(fu => fu.Speciality.SpecialityID == 1) },
                            new ExcelCellChange { Row = 22, Column = 2, Value = s.FollowUps.Count(fu => fu.Speciality.SpecialityID == 2) },
                            new ExcelCellChange { Row = 23, Column = 2, Value = s.FollowUps.Count(fu => fu.Speciality.SpecialityID == 3) },
                            new ExcelCellChange { Row = 24, Column = 2, Value = s.FollowUps.Count(fu => fu.Speciality.SpecialityID == 4) },
                            new ExcelCellChange { Row = 25, Column = 2, Value = s.FollowUps.Count(fu => fu.Speciality.SpecialityID == 5) },
                            new ExcelCellChange { Row = 26, Column = 2, Value = s.FollowUps.Count(fu => fu.Speciality.SpecialityID == 6) },
                            new ExcelCellChange { Row = 27, Column = 2, Value = s.FollowUps.Count(fu => fu.Speciality.SpecialityID == 7) },
                            new ExcelCellChange { Row = 28, Column = 2, Value = s.FollowUps.Count(fu => fu.Speciality.SpecialityID == 8) },
                            new ExcelCellChange { Row = 29, Column = 2, Value = s.FollowUps.Count(fu => fu.Speciality.SpecialityID == 9) },
                            new ExcelCellChange { Row = 30, Column = 2, Value = s.FollowUps.Count(fu => fu.Speciality.SpecialityID == 10) },
                            new ExcelCellChange { Row = 31, Column = 2, Value = s.FollowUps.Count(fu => fu.Speciality.SpecialityID == 11) },
                            new ExcelCellChange { Row = 32, Column = 2, Value = s.FollowUps.Count(fu => fu.Speciality.SpecialityID == 12) },
                            new ExcelCellChange { Row = 33, Column = 2, Value = s.FollowUps.Count(fu => fu.Speciality.SpecialityID == 13) },
                            new ExcelCellChange { Row = 8, Column = 7, Value = s.FollowUps.Count(fu => fu.FollowUpTakePart == 1) },
                            new ExcelCellChange { Row = 9, Column = 7, Value = s.FollowUps.Count(fu => fu.FollowUpTakePart == 2) },
                            new ExcelCellChange { Row = 10, Column = 7, Value = s.FollowUps.Count(fu => fu.FollowUpTakePart == 3) },
                            new ExcelCellChange { Row = 11, Column = 7, Value = s.FollowUps.Count(fu => fu.FollowUpTakePart == 4) },
                            new ExcelCellChange { Row = 12, Column = 7, Value = s.FollowUps.Count(fu => fu.FollowUpTakePart == 5) },
                            new ExcelCellChange { Row = 13, Column = 7, Value = s.FollowUps.Count(fu => fu.FollowUpTakePart == 6) },
                            new ExcelCellChange { Row = 17, Column = 7, Value = s.FollowUps.Average(fu => fu.FollowUpConfident1)??0 },
                            new ExcelCellChange { Row = 18, Column = 7, Value = s.FollowUps.Average(fu => fu.FollowUpConfident2)??0 },
                            new ExcelCellChange { Row = 19, Column = 7, Value = s.FollowUps.Average(fu => fu.FollowUpConfident3)??0 },
                            new ExcelCellChange { Row = 20, Column = 7, Value = s.FollowUps.Average(fu => fu.FollowUpConfident4)??0 },
                            new ExcelCellChange { Row = 21, Column = 7, Value = s.FollowUps.Average(fu => fu.FollowUpConfident5)??0 },
                            new ExcelCellChange { Row = 22, Column = 7, Value = s.FollowUps.Average(fu => fu.FollowUpConfident6)??0 },
                            new ExcelCellChange { Row = 23, Column = 7, Value = s.FollowUps.Average(fu => fu.FollowUpConfident7)??0 },
                            new ExcelCellChange { Row = 24, Column = 7, Value = s.FollowUps.Average(fu => fu.FollowUpConfident8)??0 },
                            new ExcelCellChange { Row = 25, Column = 7, Value = s.FollowUps.Average(fu => fu.FollowUpConfident9)??0 },
                            new ExcelCellChange { Row = 26, Column = 7, Value = s.FollowUps.Average(fu => fu.FollowUpConfident10)??0 }
                        });

            ExcelSheetInformation sheet = new ExcelSheetInformation() { Name = "Seminar follow-up", Changes = excelChanges };
            sheets.Add(sheet);
            var changes = sheets.AsQueryable();
            string temporaryFileName = Server.MapPath(@"~/temp/" + Guid.NewGuid() + ".xlsx");

            if (log.IsDebugEnabled) log.DebugFormat("Leaving Program with id {0} ta...", seminarID);

            return this.Excel("EOD_Single_Seminar_Follow-up.xlsx", temporaryFileName, Server.MapPath(@"~/templates/EOD_Single_Seminar_Follow-up.xlsx"), "Seminar follow-up", changes);
        }

        //
        // GET: /Seminar/Participants/5

        [Authorize(Roles = "Demostrator,Reporter,Administrator")]
        public ActionResult Assessments(int seminarID)
        {
            if (log.IsDebugEnabled) log.DebugFormat("Leaving Program with id {0} ta...", seminarID);

            var s = repository.Get(seminarID);
            var ta = s.TargetArea;
            var program = s.TargetArea.Program;

            List<ExcelCellChange> excelChanges = new List<ExcelCellChange>();
            List<ExcelSheetInformation> sheets = new List<ExcelSheetInformation>();

            var assessmentOrganDonor = new AssessmentLecture(s.Assessments.Average(a => a.AssessmentOrganDonorP), s.Assessments.Average(a => a.AssessmentOrganDonorC), s.Assessments.Average(a => a.AssessmentOrganDonorQ));
            var assessmentCaseStudies = new AssessmentLecture(s.Assessments.Average(a => a.AssessmentCaseStudiesP), s.Assessments.Average(a => a.AssessmentCaseStudiesC), s.Assessments.Average(a => a.AssessmentCaseStudiesQ));
            var assessmentBrainDeath = new AssessmentLecture(s.Assessments.Average(a => a.AssessmentBrainDeathP), s.Assessments.Average(a => a.AssessmentBrainDeathC), s.Assessments.Average(a => a.AssessmentBrainDeathQ));
            var assessmentSimulation = new AssessmentLecture(s.Assessments.Average(a => a.AssessmentSimulationP), s.Assessments.Average(a => a.AssessmentSimulationC), s.Assessments.Average(a => a.AssessmentSimulationQ));
            var assessmentDonor = new AssessmentLecture(s.Assessments.Average(a => a.AssessmentDonorP), s.Assessments.Average(a => a.AssessmentDonorC), s.Assessments.Average(a => a.AssessmentDonorQ));
            var assessmentApproach = new AssessmentLecture(s.Assessments.Average(a => a.AssessmentApproachP), s.Assessments.Average(a => a.AssessmentApproachC), s.Assessments.Average(a => a.AssessmentApproachQ));
            var assessmentInterviews = new AssessmentLecture(s.Assessments.Average(a => a.AssessmentInterviewsP), s.Assessments.Average(a => a.AssessmentInterviewsC), s.Assessments.Average(a => a.AssessmentInterviewsQ));
            var assessmentRetrieval = new AssessmentLecture(s.Assessments.Average(a => a.AssessmentRetrievalP), s.Assessments.Average(a => a.AssessmentRetrievalC), s.Assessments.Average(a => a.AssessmentRetrievalQ));

            excelChanges.AddRange(new List<ExcelCellChange>
                    {
                        new ExcelCellChange { Row = 2, Column = 2, Value = program.ProgramName },
                        new ExcelCellChange { Row = 3, Column = 2, Value = "ASSESSMENTS - " + s.FriendlyTitle },
                        new ExcelCellChange { Row = 5, Column = 3, Value = s.Assessments.Count },

                        new ExcelCellChange { Row = 9, Column = 3, Value = assessmentOrganDonor.Presentation },
                        new ExcelCellChange { Row = 9, Column = 4, Value = assessmentOrganDonor.Contents },
                        new ExcelCellChange { Row = 9, Column = 5, Value = assessmentOrganDonor.QuestionsAnswered },

                        new ExcelCellChange { Row = 10, Column = 3, Value = assessmentCaseStudies.Presentation },
                        new ExcelCellChange { Row = 10, Column = 4, Value = assessmentCaseStudies.Contents },
                        new ExcelCellChange { Row = 10, Column = 5, Value = assessmentCaseStudies.QuestionsAnswered },

                        new ExcelCellChange { Row = 11, Column = 3, Value = assessmentBrainDeath.Presentation },
                        new ExcelCellChange { Row = 11, Column = 4, Value = assessmentBrainDeath.Contents },
                        new ExcelCellChange { Row = 11, Column = 5, Value = assessmentBrainDeath.QuestionsAnswered },

                        new ExcelCellChange { Row = 12, Column = 3, Value = assessmentSimulation.Presentation },
                        new ExcelCellChange { Row = 12, Column = 4, Value = assessmentSimulation.Contents },
                        new ExcelCellChange { Row = 12, Column = 5, Value = assessmentSimulation.QuestionsAnswered },

                        new ExcelCellChange { Row = 13, Column = 3, Value = assessmentDonor.Presentation },
                        new ExcelCellChange { Row = 13, Column = 4, Value = assessmentDonor.Contents },
                        new ExcelCellChange { Row = 13, Column = 5, Value = assessmentDonor.QuestionsAnswered },

                        new ExcelCellChange { Row = 14, Column = 3, Value = assessmentApproach.Presentation },
                        new ExcelCellChange { Row = 14, Column = 4, Value = assessmentApproach.Contents },
                        new ExcelCellChange { Row = 14, Column = 5, Value = assessmentApproach.QuestionsAnswered },

                        new ExcelCellChange { Row = 15, Column = 3, Value = assessmentInterviews.Presentation },
                        new ExcelCellChange { Row = 15, Column = 4, Value = assessmentInterviews.Contents },
                        new ExcelCellChange { Row = 15, Column = 5, Value = assessmentInterviews.QuestionsAnswered },

                        new ExcelCellChange { Row = 16, Column = 3, Value = assessmentRetrieval.Presentation },
                        new ExcelCellChange { Row = 16, Column = 4, Value = assessmentRetrieval.Contents },
                        new ExcelCellChange { Row = 16, Column = 5, Value = assessmentRetrieval.QuestionsAnswered },

                        new ExcelCellChange { Row = 19, Column = 3, Value = s.Assessments.Average(a => a.AssessmentOrganization) },
                        new ExcelCellChange { Row = 20, Column = 3, Value = s.Assessments.Average(a => a.AssessmentTeaching) },
                        new ExcelCellChange { Row = 21, Column = 3, Value = s.Assessments.Average(a => a.AssessmentSupport) },
                        new ExcelCellChange { Row = 22, Column = 3, Value = s.Assessments.Average(a => a.AssessmentSecretariat) },

                        new ExcelCellChange { Row = 25, Column = 3, Value = s.Assessments.Average(a => a.AssessmentPreRegistration) },
                        new ExcelCellChange { Row = 26, Column = 3, Value = s.Assessments.Average(a => a.AssessmentPostRegistration) },

                        new ExcelCellChange { Row = 28, Column = 3, Value = s.Assessments.Average(a => a.AssessmentApplicability) },

                        new ExcelCellChange { Row = 30, Column = 3, Value = s.Assessments.Average(a => a.AssessmentOverall) },
                        });

         

            ExcelSheetInformation sheet = new ExcelSheetInformation() { Name = "Seminar assessments", Changes = excelChanges };
            sheets.Add(sheet);
            var changes = sheets.AsQueryable();
            string temporaryFileName = Server.MapPath(@"~/temp/" + Guid.NewGuid() + ".xlsx");

            if (log.IsDebugEnabled) log.DebugFormat("Leaving Program with id {0} ta...", seminarID);

            return this.Excel("EOD_Single_Seminar_Assessments.xlsx", temporaryFileName, Server.MapPath(@"~/templates/EOD_Single_Seminar_Assessments.xlsx"), "Seminar assessments", changes);
        }
        #endregion

    }
}
